package MyServlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Candidate;
import model.Comment;
import model.Position;
import service.ServiceException;
import ServletService.Service;

/**
 * Servlet implementation class CrudCandidate
 */
public class CrudCandidate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CrudCandidate() {
		super();
		// TODO Auto-generated constructor stub
	}

	private String option;
	private Service service;
	private Candidate candidate;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		option = request.getParameter("option");
		System.out.println(option);
		switch (option) {
		case "add": {
			String firstname = request.getParameter("firstname");
			String lastname = request.getParameter("lastname");
			String email = request.getParameter("email");
			String mobile = request.getParameter("mobile");
			String phone = request.getParameter("phone");
			String address = request.getParameter("address");
			String relocate = request.getParameter("realocate");
			boolean rel;
			if (relocate.equals("Yes")) {
				rel = true;
			} else {
				rel = false;
			}
			List<Comment> comments = new ArrayList<Comment>();
			String CV = "cv";

			Candidate can = new Candidate(firstname, lastname, email, mobile,
					phone, address, comments, rel, CV);
			Service.getCandidateService().insertCandidate(can);
			break;
		}
		case "update": {
			Candidate can = Service.getCandidateService().getCandidateByID(
					Long.parseLong(request.getParameter("id")));
			if (!request.getParameter("firstname").equals("")) {
				can.setFirstName(request.getParameter("firstname"));
			}
			if (!request.getParameter("lastname").equals("")) {
				can.setLastName(request.getParameter("lastname"));
			}
			if (!request.getParameter("email").equals("")) {
				can.setEmail(request.getParameter("email"));
			}
			if (!request.getParameter("mobileno").equals("")) {
				can.setMobileNo(request.getParameter("mobileno"));
			}
			if (!request.getParameter("telephone").equals("")) {
				can.setTelephoneNo(request.getParameter("telephone"));
			}
			if (!request.getParameter("address").equals("")) {
				can.setAddress(request.getParameter("address"));
			}
			Service.getCandidateService().updateCandidate(can);
			break;
		}
		case "delete": {
			Candidate can = Service.getCandidateService().getCandidateByID(
					Long.parseLong(request.getParameter("id")));
			Service.getCandidateService().deleteCandidate(can);
			break;
		}
		case "getAll": {
			Service.getCandidateService().getAll();
			break;
		}
		case "addToPosition": {
			Candidate can;
			can = Service.getCandidateService().getCandidateByID(
					Long.parseLong(request.getParameter("id")));
			// Service.getPositionService().addCandidateToPosition(request.positionid+can);
			break;
		}

		}
		RequestDispatcher rd = request
				.getRequestDispatcher("/login");
		rd.forward(request, response);
	}
}
